<?php
	class BakdbAction extends CommonAction {
		public function index(){
			$data_dir = './backup/';
			if ( !empty( $_GET['action'] ) ){
				import( 'ORG.Net.MySQLReback' );
				$config = array(
					'host'			=> C('DB_HOST'),
					'port'			=> C('DB_PORT'),
					'userName'		=> C('DB_USER'),
					'userPassword'	=> C('DB_PWD'),
					'dbprefix'		=> C('DB_PREFIX'),
					'charset'		=> 'UTF8',
					'path'			=> $data_dir,
					'isCompress'	=> 0,
					'isDownload'	=> 0
				);
				
				$mr = new MySQLReback( $config );
				$mr->setDBName( C('DB_NAME') );
				
				switch ( $_GET['action'] ) {
					case 'backup':
						$mr->backup();
						$this->assign( "jumpUrl", U('index', array('tag'=>$_GET['tag'])) );
						$this->success( '数据备份完成' );
						break;
					
					case 'reduction':
						$mr->recover( $_GET['file'] );
						$this->assign( "jumpUrl", U('index', array('tag'=>$_GET['tag'])) );
						$this->success( '数据库还原成功' );
						break;
						
					case 'del':
						if ( @unlink( $data_dir . $_GET['file'] ) ){
							$this->assign( "jumpUrl", U('index', array('tag'=>$_GET['tag'])) );
							$this->success( '删除完成' );
						}else{
							$this->error( '删除失败' );
						}
						break;
						
					case 'download':
						function downloadFile( $file_name ){
							ob_end_clean();
							header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
							header( "Content-Description: File Transfer" );
							header( "Content-Type: application/octet-stream" );
							header( "Content-Length: " . filesize( $file_name ) );
							header( "Content-Disposition: attachment; filename=" . basename( $file_name ) );
							readfile( $file_name );
						}
						downloadFile( $data_dir . $_GET['file'] );
						exit();
						break;
				}
			}

			$this->display();
		}
	}
?>
